/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.dialog;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQDialogUtils
{

	/**
	 * Create a JQDialogBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQDialogBuilder
	 */
	public static JQDialogBuilder create(String id, String key)
	{
		return new JQDialogBuilder(new JQDialog(id, (key == null ? JQDialogPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQDialogBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQDialogBuilder
	 */
	public static JQDialogBuilder create(String id)
	{
		return new JQDialogBuilder(new JQDialog(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQDialogBuilder
	{
		/** JQuery component */
		private JQDialog component;
		/** JQuery default options */
		private JQDialogOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQDialogBuilder(JQDialog component)
		{
			this.component = component;
			this.options = new JQDialogOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the autoOpen option value
		 * 
		 * @param autoOpen
		 *            dialog autoOpen
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder autoOpen(boolean autoOpen)
		{
			options.setAutoOpen(autoOpen);
			return this;
		}

		/**
		 * Sets the draggable option value
		 * 
		 * @param draggable
		 *            dialog draggable
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder draggable(boolean draggable)
		{
			options.setDraggable(draggable);
			return this;
		}

		/**
		 * Sets the modal option value
		 * 
		 * @param modal
		 *            dialog modal
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder modal(boolean modal)
		{
			options.setModal(modal);
			return this;
		}

		/**
		 * Sets the resizable option value
		 * 
		 * @param resizable
		 *            dialog resizable
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder resizable(boolean resizable)
		{
			options.setResizable(resizable);
			return this;
		}

		/**
		 * Sets the stack option value
		 * 
		 * @param stack
		 *            dialog stack
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder stack(boolean stack)
		{
			options.setStack(stack);
			return this;
		}

		/**
		 * Adds a button to the dialog
		 * 
		 * @param key
		 *            button key (also text value)
		 * @param callback
		 *            callback function on button click
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder addButton(String key, String callback)
		{
			options.addButton(key, callback);
			return this;
		}

		/**
		 * Removes the button with specified key from the dialog
		 * 
		 * @param key
		 *            button key
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder removeButton(String key)
		{
			options.removeButton(key);
			return this;
		}

		/**
		 * Sets the dialogClass option value
		 * 
		 * @param dialogClass
		 *            dialog dialogClass
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder dialogClass(String dialogClass)
		{
			options.setDialogClass(dialogClass);
			return this;
		}

		/**
		 * Sets the hide animation option value
		 * 
		 * @param hide
		 *            dialog hide animation
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder hide(String hide)
		{
			options.setHide(hide);
			return this;
		}

		/**
		 * Sets the show animation option value
		 * 
		 * @param show
		 *            dialog show animation
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder show(String show)
		{
			options.setShow(show);
			return this;
		}

		/**
		 * Sets the title option value
		 * 
		 * @param title
		 *            dialog title
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder title(String title)
		{
			options.setTitle(title);
			return this;
		}

		/**
		 * Sets the overlay option value
		 * 
		 * @param overlay
		 *            dialog overlay
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder overlay(String overlay)
		{
			options.setOverlay(overlay);
			return this;
		}

		/**
		 * Sets the width option value
		 * 
		 * @param width
		 *            dialog width
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder width(int width)
		{
			options.setWidth(width);
			return this;
		}

		/**
		 * Sets the min width option value
		 * 
		 * @param minWidth
		 *            dialog min width
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder minWidth(int minWidth)
		{
			options.setMinWidth(minWidth);
			return this;
		}

		/**
		 * Sets the max width option value
		 * 
		 * @param maxWidth
		 *            dialog max width
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder maxWidth(int maxWidth)
		{
			options.setMaxWidth(maxWidth);
			return this;
		}

		/**
		 * Sets the height option value
		 * 
		 * @param height
		 *            dialog height
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder height(int height)
		{
			options.setHeight(height);
			return this;
		}

		/**
		 * Sets the min height option value
		 * 
		 * @param minHeight
		 *            dialog minHeight
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder minHeight(int minHeight)
		{
			options.setMinHeight(minHeight);
			return this;
		}

		/**
		 * Sets the max height option value
		 * 
		 * @param maxHeight
		 *            dialog maxHeight
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder maxHeight(int maxHeight)
		{
			options.setMaxHeight(maxHeight);
			return this;
		}

		/**
		 * Sets the position option value
		 * 
		 * @param position
		 *            dialog position
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder position(Object[] position)
		{
			options.setPosition(position);
			return this;
		}

		/**
		 * Sets the open function
		 * 
		 * @param openScript
		 *            dialog open function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder openScript(String openScript)
		{
			options.setOpenScript(openScript);
			return this;
		}

		/**
		 * Sets the focus function
		 * 
		 * @param focusScript
		 *            dialog focus function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder focusScript(String focusScript)
		{
			options.setFocusScript(focusScript);
			return this;
		}

		/**
		 * Sets the close function
		 * 
		 * @param closeScript
		 *            dialog close function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder closeScript(String closeScript)
		{
			options.setCloseScript(closeScript);
			return this;
		}

		/**
		 * Sets the dragStart function
		 * 
		 * @param dragStartScript
		 *            dialog drag start function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder dragStartScript(String dragStartScript)
		{
			options.setDragStartScript(dragStartScript);
			return this;
		}

		/**
		 * Sets the drag function
		 * 
		 * @param dragScript
		 *            dialog drag function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder dragScript(String dragScript)
		{
			options.setDragScript(dragScript);
			return this;
		}

		/**
		 * Sets the dragStop function
		 * 
		 * @param dragStopScript
		 *            dialog drag stop function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder dragStopScript(String dragStopScript)
		{
			options.setDragStopScript(dragStopScript);
			return this;
		}

		/**
		 * Sets the resizeStart function
		 * 
		 * @param resizeStartScript
		 *            dialog resize start function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder resizeStartScript(String resizeStartScript)
		{
			options.setResizeStartScript(resizeStartScript);
			return this;
		}

		/**
		 * Sets the resize function
		 * 
		 * @param resizeScript
		 *            dialog resize function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder resizeScript(String resizeScript)
		{
			options.setResizeScript(resizeScript);
			return this;
		}

		/**
		 * Sets the resize stop function
		 * 
		 * @param resizeStopScript
		 *            dialog resize stop function
		 * @return JQDialogBuilder
		 */
		public JQDialogBuilder resizeStopScript(String resizeStopScript)
		{
			options.setResizeStopScript(resizeStopScript);
			return this;
		}

		/**
		 * @return JQDialog
		 */
		public JQDialog build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}